package com.china08.yxyapi.api.manage.tec.teaching.homework.workassign.question;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.user.YxyTeacherCenter;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkUserCenterRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

/**
 * 拿到用户学段 ， 和 版本
 * 
 * @author renruihu
 *
 */
@RestController
@CrossOrigin
public class HomeworkInitController extends YxyApiBaseController {

    @Autowired
    private HomeworkUserCenterRepository homeworkUserCenterRepository;

    @ApiOperation(value = "作业系统初始化上次操作的版本")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/workassign/question/version/init", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        YxyProfile profile = getProfile(request, response);
        String userId = profile.getId();
        YxyTeacherCenter center = homeworkUserCenterRepository.findByTeacherIdAndStageAndSubject(userId, reqModel.phase,
                reqModel.subject);
        RespModel model = new RespModel();
        Model version = new Model();
        BookModel book = new BookModel();
        Model bookcontent = new Model();
        if (center != null) {
            version.id = center.getVersion();
            version.name = center.getVersionName();
            //
            book.bookId = center.getBookId();
            book.name = center.getBookName();
            //
            bookcontent.id = center.getBookContent();
            bookcontent.name = center.getBookContentName();

            model.id = center.getVersion();
            model.name = center.getVersionName();
        } else {
            center = new YxyTeacherCenter();
            center.setTeacherId(userId);
            center.setStage(reqModel.phase);
            center.setSubject(reqModel.subject);
            homeworkUserCenterRepository.save(center);
        }
        model.version = version;
        model.book = book;
        model.bookContent = bookcontent;
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String subject;
        public String phase;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请填写学段");
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "请填写学科");
        }

    }

    protected static class BookModel {
        public String bookId;
        public String name;
    }

    protected static class Model {
        public String id;
        public String name;
    }

    protected static class RespModel {
        public Model version;
        public BookModel book;
        public Model bookContent;

        @Deprecated
        public String id;

        @Deprecated
        public String name;
    }

}
